package com.shuai.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.shuai.adapter.MemoryListAdapter;
import com.shuai.edit.MemoryEditActivity;
import com.shuai.edit.TaskEditActivity;
import com.shuai.sql.SQL;
import com.shuai.stickynote.EditActivity;
import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.R;
import com.shuai.tool.ClassFactory;
import com.shuai.tool.SendTool;

import java.util.ArrayList;

public class MemmoryFragemnt extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private FloatingActionButton btn_add;
    private String group = "";
    private ArrayList<ArrayList<MemoryNote>> memorys;
    private MemoryListAdapter adapter;
    private SQL sql;
    private String TAG = "fuck";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragemnt_memmory,container,false);
        init();
        return view;
    }
    private void init() {
        recyclerView = view.findViewById(R.id.memory_list);
        btn_add = view.findViewById(R.id.memory_btn_add);
        sql = EditActivity.sql;
        getData();
        setList();
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MemoryEditActivity.class);
                intent.putExtra("group",group);
                startActivity(intent);
            }
        });
    }
    private void setList(){
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        adapter = new MemoryListAdapter(getContext());
        adapter.replaceAll(memorys);
        recyclerView.setAdapter(adapter);
        adapter.setOnRecyItemClickListener(new MemoryListAdapter.OnRecyItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getActivity(), MemoryEditActivity.class);
                intent.putExtra("group",memorys.get(position).get(0).getGroup());
                startActivity(intent);
            }
        });
        adapter.setOnRecyItemLongClickListener(new MemoryListAdapter.OnRecyItemLongClickListener() {
            @Override
            public void onClick(View view, final int position) {
                View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_plant_long_click,null);
                TextView btn_cancle = dialogView.findViewById(R.id.btn_cancel);
                TextView btn_delete = dialogView.findViewById(R.id.btn_delete);
                TextView btn_send = dialogView.findViewById(R.id.btn_send);
                if (EditActivity.widgetId == 0){
                    btn_send.setVisibility(View.GONE);
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext()).setView(dialogView);
                final Dialog dialog = builder.create();
                dialog.setContentView(dialogView);
                dialog.show();
                btn_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sql.deleteMemoryByGroup(memorys.get(position).get(0).getGroup());
                        Toast.makeText(getContext(), "删除成功~", Toast.LENGTH_SHORT).show();
                        onResume();
                        dialog.dismiss();
                    }
                });
                btn_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SendTool.sendToWidget(getContext(), ClassFactory.getClass(EditActivity.size),
                                EditActivity.widgetId, SendTool.WIDGET_ACTION_MENORY,
                                memorys.get(position).get(0).getGroup(),SendTool.TYPE_MEMORY);
                        EditActivity.widgetId = 0;
                        EditActivity.size = "";
                        dialog.dismiss();
                        Toast.makeText(getContext(), "已发送到桌面小部件~", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    private void getData(){
        memorys = sql.selectMemoryByAllSorted();
    }

    @Override
    public void onResume() {
        getData();
        adapter.replaceAll(memorys);
        super.onResume();
    }
}
